package com.example.controller;

import com.example.common.Result;
import com.example.entity.Warehouse;
import com.example.service.WarehouseService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/warehouse")
public class WarehouseController {

    @Resource
    private WarehouseService warehouseService;

    @PostMapping("/add")
    public Result add(@RequestBody Warehouse warehouse) {
        warehouseService.add(warehouse);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        warehouseService.deleteById(id);
        return Result.success();
    }

    @PutMapping("/update")
    public Result update(@RequestBody Warehouse warehouse) {
        warehouseService.updateById(warehouse);
        return Result.success();
    }

    @GetMapping("/selectAll")
    public Result selectAll() {
        List<Warehouse> warehouseList = warehouseService.selectAll();
        return Result.success(warehouseList);
    }

    @GetMapping("/select/{id}")
    public Result select(@PathVariable Integer id) {
        Warehouse warehouse = warehouseService.selectById(id);
        return Result.success(warehouse);
    }

    /**
     * 分页查询
     * pageNum: 当前的页码
     * pageSize: 每页的个数
     */
    @GetMapping("/selectPage")
    public Result selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize){
        PageInfo<Warehouse> pageInfo = warehouseService.selectPage(pageNum, pageSize);
        return Result.success(pageInfo);
    }
}
